﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart.Extension;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using System.Reflection;

namespace WMSSoft.WinKStart.ViewModel
{
    public class CContentRegionsViewModel : SettingBaseListViewModel
    {
        List<cRegion> _Data = null;
        System.Windows.Controls.ListView _DataListView = null;
        cRegion _SelectedDataItem = null;
        bool _CanWorkWithItems = false;

        public CContentRegionsViewModel() 
        {
            PropertyChanged += CContentRegionsViewModel_PropertyChanged;

            ViewModel_AddClick += CContentRegionsViewModel_ViewModel_AddClick;
            ViewModel_DeleteClick += CContentRegionsViewModel_ViewModel_DeleteClick;
            ViewModel_EditClick += CContentRegionsViewModel_ViewModel_EditClick;

        }
        
        public CContentRegionsViewModel(List<cRegion> Data) 
        {
            PropertyChanged += CContentRegionsViewModel_PropertyChanged;
            //Commands
            _Data = Data;

            ViewModel_AddClick += CContentRegionsViewModel_ViewModel_AddClick;
            ViewModel_DeleteClick += CContentRegionsViewModel_ViewModel_DeleteClick;
            ViewModel_EditClick += CContentRegionsViewModel_ViewModel_EditClick;

        }

        public CContentRegionsViewModel(string ViewName)
        {
            PropertyChanged += CContentRegionsViewModel_PropertyChanged;
            base.ViewName = ViewName;
        }

        #region Properties

        //public CSingleCommandViewModel Cmd_AddRegion
        //{
        //    get { return m_oCommands[0]; }
        //}

        //public CSingleCommandViewModel Cmd_EditRegion
        //{
        //    get { return m_oCommands[1]; }
        //}

        //public CSingleCommandViewModel Cmd_DeleteRegion
        //{
        //    get { return m_oCommands[2]; }
        //}

        public List<cRegion> Data
        {
            get { return _Data; }
        }

        public cRegion SelectedDataItem
        {
            get { return _SelectedDataItem; }
            set 
            { 
                _SelectedDataItem = value;
                OnPropertyChanged("SelectedDataItem");
            }
        }

        public bool CanWorkWithItems
        {
            get { return _CanWorkWithItems; }
            set
            {
                _CanWorkWithItems = value;
                OnPropertyChanged("CanWorkWithItems");
            }
        }
        #endregion

        #region Commands

        void CContentRegionsViewModel_ViewModel_AddClick(object sender, EventArgs e)
        {
            try
            {
         
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddClick.Command).CommandParameter != null)
                {
                    _DataListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddClick.Command).CommandParameter;

                    SelectedDataItem = null;
                    Setting_NewEditRegionViewModel tmpViewModel = new Setting_NewEditRegionViewModel();
                    tmpViewModel.DataChanged += tmpViewModel_DataChanged;
                    Settings_NewEditRegion tmpDialog = new Settings_NewEditRegion();
                    tmpDialog.DataContext = tmpViewModel;
                    if (tmpDialog.ShowDialog() == true) 
                    {
                        cRegion tmpRegion = ((Setting_NewEditRegionViewModel)tmpDialog.DataContext).Data;
                        //first, search for a Region with tmpRegion.Name
                        //if here is no region with this name add new
                        if (FindRegion(tmpRegion.Name) == false)
                        {
                            if (_SelectedDataItem == null)
                            {
                                Data.Add(tmpRegion);
                                
                            }
                            else
                            {
                                _SelectedDataItem = tmpRegion;
                            }

                            if (_DataListView != null) _DataListView.Items.Refresh();
                        }
                        else
                        {
                            System.Windows.MessageBox.Show(LanguageStrings.GetText(InfoMessages.INFO_CONTENT_REGION__REGION_ALREADY_EXISTS, 
                                                           EStringTypes.stMessage), 
                                                           LanguageStrings.GetText("Info"), 
                                                           System.Windows.MessageBoxButton.OK);
                        }

                        _DataListView = null;
                    }
                }
            } catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("An error occurred on adding Region!", EStringTypes.stErrorMesssage), 
                                              Ex, true, EMessageTypes.Error);
            }           
        }

        void CContentRegionsViewModel_ViewModel_EditClick(object sender, EventArgs e)
        {
            try
            {
                
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditClick.Command).CommandParameter != null)
                {
                    _DataListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditClick.Command).CommandParameter;

                    Setting_NewEditRegionViewModel tmpViewModel = new Setting_NewEditRegionViewModel(_SelectedDataItem);
                    tmpViewModel.DataChanged += tmpViewModel_DataChanged;
                    Settings_NewEditRegion tmpDialog = new Settings_NewEditRegion();
                    tmpDialog.DataContext = tmpViewModel;
                    if (tmpDialog.ShowDialog() == true) 
                    {
                        _SelectedDataItem = ((Setting_NewEditRegionViewModel)tmpDialog.DataContext).Data;
                        if (_DataListView != null) _DataListView.Items.Refresh();
                    }

                    _DataListView = null;
                }
            } catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("An error occurred on editing Region!", EStringTypes.stErrorMesssage),
                                              Ex, true, EMessageTypes.Error);
            } 
        }

        void CContentRegionsViewModel_ViewModel_DeleteClick(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Controls.ListView tmpListBox = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteClick.Command).CommandParameter != null)
                {
                    tmpListBox = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteClick.Command).CommandParameter;

                    if (_SelectedDataItem != null)
                    {
                        string sMessage = LanguageStrings.GetText(QuestionMessages.QUESTION_CONTENT_REGION__DELETE_REGION, EStringTypes.stQuestion);
                        sMessage = sMessage.Replace("###REGIONNAME###", _SelectedDataItem.Name);
                        if (System.Windows.MessageBox.Show(sMessage, LanguageStrings.GetText("Question"),
                                                       System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                        {
                            Data.Remove(_SelectedDataItem);
                            if (tmpListBox != null) tmpListBox.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("An error occurred on deleting Region!", EStringTypes.stErrorMesssage),
                                              Ex, true, EMessageTypes.Error);
            } 
        }

        /// <summary>
        /// will raised if the user has applied Data in the Dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tmpViewModel_DataChanged(object sender, EventArgs e)
        {
            try
            {
                if (_SelectedDataItem != null)
                {
                    //Useer edit a Region
                    _SelectedDataItem = ((Setting_NewEditRegionViewModel)sender).Data;
                    
                }
                else
                {
                    //user add a new region
                    if (FindRegion(((Setting_NewEditRegionViewModel)sender).Data.Name) == false) 
                    {
                        Data.Add(((Setting_NewEditRegionViewModel)sender).Data);
                        _SelectedDataItem = Data[Data.Count - 1];
                        
                    }
                }
                if (_DataListView != null) _DataListView.Items.Refresh();

            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("An error occurred on applying Region!", EStringTypes.stErrorMesssage),
                                                              Ex, true, EMessageTypes.Error);
            }
        }

        #endregion

        void CContentRegionsViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "SelectedDataItem")
                    SetCanWorkWithItems();
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on changed Property '" + e.PropertyName + "'!", Ex, EMessageTypes.Error);
            }
        }

        void SetCanWorkWithItems()
        {
            if (_SelectedDataItem != null)
                CanWorkWithItems = true;
            else
                CanWorkWithItems = false;

        }

        /// <summary>
        /// Test to exist Region-Name in Region-List
        /// </summary>
        /// <param name="Name">Region-Name to test</param>
        /// <returns>true if Region-Name doest exist otherwise false</returns>
        bool FindRegion(string Name)
        {
            try
            {
                if (_Data != null)
                {
                    var ListResult = (from Region in _Data where Region.Name.ToUpper() == Name.ToUpper() select Region);
                    if (ListResult != null && ListResult.Count() > 0)
                        return true;
                    else
                        return false;
                }
                else return false;
            }
            catch (Exception Ex)
            {
                throw new Exception("Error on find Region", Ex);
            }
        }
    }
}
